package com.itnoob.demo1;

import java.util.HashMap;
import java.util.Map;

/**
 * 享元工厂
 * @author zhoujun
 * @date 2025/3/21 21:53
 */
public class CoffeeFactory {

    private final Map<String,Coffee> coffeeMap = new HashMap<>();

    public Coffee getCoffee(String name,double price ){
        //以name来作为key来查找coffeeMap中是否已经存在相同名称的咖啡对象
        Coffee coffee = coffeeMap.get(name);
        if (coffee == null){
            coffee = new ConcreteCoffee(name,price);
            coffeeMap.put(name,coffee);
            System.out.println("创建了"+ name + "实例");
        }
        return  coffee;   //如果存在,就返回已有的对象
    }
}
